﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using HR60_PanelSoft.Model;
using HR60_PanelSoft.View.Base;
using HR60_PanelSoft.View.Controls;

namespace HR60_PanelSoft.View.TabForms
{
    //GC: [g1]bug2185/2186/2187[DeviceForm]
    //GC: [g2]bug2189/2190[DeviceForm]
    //GC: [g3]bug2192/2194/2203/2204/2205[DeviceForm]

	public partial class DeviceForm : BaseForm
	{
		public DeviceForm()
		{
			InitializeComponent();
		}

		// 绑定数据
		public override void refreshFormData(System.Collections.Generic.IDictionary<string, object> dataMap)
		{
			Device device = (Device)dataMap["device"];

			// 昵称
			txtNickName.Value = device.nickName;

			// 类型分组列表
			selGroup.bindData(deviceTypeGroups);
			selGroup.Value = device.type.group;

			// 类型列表
			selType.bindData(deviceTypes[device.type.group], "name", "name");
			//selGroup.Value = device.type.group; //GC:  此处有bug
            selType.Value = device.type.name;  //GC:此处有bug
			// 分机
			cleanSubEdit();

			if (device.type.subDeviceMax > 0)
			{
				divSubListTitle.Visible = true;
				subLine.Visible = true;
				foreach (Device sub in device.childDevices)
					appendSubEdit(sub);
			}
			else
			{
				divSubListTitle.Visible = false;
				subLine.Visible = false;
			}

			// 厂商
			if (device.type.brands.Count == 0)
			{
				selBrand.Visible = false;
			}
			else
			{
                /* [g3.s1]bug2192 */

                //selBrand.bindData(device.type.brands, "name");
                //selBrand.Value = device.brand;
                selBrand.bindData(device.type.brands, "name", "name");
                selBrand.Value = device.brand.name;
                /* end of [g3.s1] */
				selBrand.Visible = true;
			}

		}

		// 数据修改事件
		public override void onFormChanged(BaseFormControl sender, System.EventArgs e)
		{
            /* [g2.s1]先判断昵称是否为空 */
            if (sender != null && e != null && ((ValueChangedEventArgs)e).Type == EventType.TitleTextBox)
            {
                if (sender.Name == "txtNickName")
                {
                    /* 处理为空的情况 */
                    if (((TitleTextBox)sender).Value.ToString() == string.Empty)
                    {
                        View.ViewUtils.showAlert("设备昵称不能为空！\r\n");
                        /* 恢复 */
                        foreach (Control c in txtNickName.GetCoreControls())
                        {
                            if (c is TextBox)
                                ((TextBox)c).Undo();
                        }
                        return;
                    }

                    /* 处理同组不能重复的情况 */
                    string tempStr = ((TitleTextBox)sender).Value.ToString();
                    DeviceGroup tempDg = findGroupByDeviceUuid(((Device)this.bindDataMap["device"]).uuid);
                    foreach (Device d in tempDg.devices)
                    {
                        if (d.nickName == tempStr && tempStr != ((Device)this.bindDataMap["device"]).nickName)
                        {
                            View.ViewUtils.showAlert("同组内的设备昵称不能重复！\r\n");
                            /* 恢复 */
                            foreach (Control c in txtNickName.GetCoreControls())
                            {
                                if (c is TextBox)
                                    ((TextBox)c).Undo();
                            } 
                            return;
                        }
                    }

                }
            }
            /* end of [g2.s1] */
			if (sender == selGroup)
			{
				isBindDataFlag = false;
				selType.bindData(deviceTypes[selGroup.Value.ToString()], "name", "name");
				isBindDataFlag = true;
				return;
			}
			windowMain.doSaveDevice((Device)this.bindDataMap["device"], this.getFields());
		}

		#region 分机列表操作

		// 分机控件列表
		IList<DivPanel> subDivs = new List<DivPanel>();

		// 添加
		private void appendSubEdit(Device sub)
		{
			DivPanel div = new DivPanel();
			div.Tag = sub;
			
			// 昵称
			TitleTextBox txtSubName = new TitleTextBox();
			txtSubName.Title = "分机" + (subDivs.Count + 1);
			txtSubName.Value = sub.nickName;
			txtSubName.Id = "subNickNames[]";
			div.Controls.Add(txtSubName);
			
			// 删除按钮
			Button btnDelSub = new Button();
			btnDelSub.Text = "删除";
			btnDelSub.Tag = sub;
			btnDelSub.Click += new EventHandler(btnDelSub_Click);
			div.Controls.Add(btnDelSub);

			// 添加按钮
			Button btnAddSub = new Button();
			btnAddSub.Text = "添加";
			btnAddSub.Click += new EventHandler(btnAddSub_Click);
			div.Controls.Add(btnAddSub);

			// 隐藏上一个
			if (subDivs.Count != 0) subDivs.Last().Controls[2].Visible = false;

			subDivs.Add(div);
			mainPanel.Controls.Add(div);
		}

		// 删除
		private void removeSubEdit(Device sub)
		{
			DivPanel tag = null;
			bool findFlag = false;
			isBindDataFlag = false;
			for (int i = 0; i < subDivs.Count; i++)
			{
				DivPanel div = subDivs[i];
				if (div.Tag == sub)
				{
					tag = div;
					findFlag = true;
					continue;
				}
				if (findFlag)
				{
					TitleTextBox txt = (TitleTextBox)div.Controls[0];
					txt.Title = "分机" + i;
					if(txt.Value.ToString() == "分机" + (i + 1))
						txt.Value = "分机" + i;
				}
			}
			isBindDataFlag = true;

			mainPanel.Controls.Remove(tag);
			subDivs.Remove(tag);
			//subDivs.Last().Controls[2].Visible = true; //GC: 此处应该存在bug
            if(0 < subDivs.Count)
            subDivs.Last().Controls[2].Visible = true; //GC: 此处应该存在bug

		}

		private void btnAddSub_Click(object sender, EventArgs e)
		{
			Device sub = windowMain.doAddNewSubDevice((Device)this.bindDataMap["device"]);

            /* [g1.s1]判断是否可以创建 */
            if (sub == null)
                return;
            /* end of [g1.s1] */
			appendSubEdit(sub);
			onFormChanged(null, null);
		}

		void btnDelSub_Click(object sender, EventArgs e)
		{
			Device sub = (Device)((Control)sender).Tag;
            /* [g1.s2] */
            //windowMain.doRemoveSubDevice((Device)this.bindDataMap["device"], sub);
            if (!windowMain.doRemoveSubDevice((Device)this.bindDataMap["device"], sub))
                return;
            /* end of [g1.s2] */
			removeSubEdit(sub);
			onFormChanged(null, null);
		}

		// 清除
		private void cleanSubEdit()
		{
			foreach (Control div in subDivs)
			{
				mainPanel.Controls.Remove(div);
				div.Dispose();
			}
			subDivs.Clear();
		}

		#endregion

	}
}
